package com.example.logistics_manage.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.logistics_manage.Utils.IsEmptyExam;
import com.example.logistics_manage.mapper.VehicleMapper;
import com.example.logistics_manage.pojo.User;
import com.example.logistics_manage.pojo.Vehicle;
import com.example.logistics_manage.service.IVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VehicleService extends ServiceImpl<VehicleMapper, Vehicle> implements IVehicleService {
    @Autowired
    VehicleMapper vehicleMapper;

    @Autowired
    IsEmptyExam isEmptyExam;

    @Override
    public IPage<Vehicle> findAllVehicle(Integer current,Integer size) {
        return vehicleMapper.findAllVehicle(Page.of(current, size));
    }

    @Override
    public Vehicle findVehicleById(Integer vehicleId) {
        return getById(vehicleId);
    }

    @Override
    public void updateVehicleById(Vehicle vehicle) {
        vehicleMapper.updateVehicleById(vehicle);
    }

    @Override
    public boolean deleteVehicleById(Integer vehicle) {
        return removeById(vehicle);
    }

    @Override
    public boolean addVehicleFirst(Vehicle vehicle) {
        return save(vehicle);
    }

    @Override
    public IPage<Vehicle> findByData(Vehicle vehicle, Integer current, Integer size) {
        if(isEmptyExam.examVehicleData(vehicle)){
            return vehicleMapper.findAllVehicle(Page.of(current, size));
        }
        if (vehicle.getVehicleNum() != null && !vehicle.getVehicleNum() .equals("")){
            return vehicleMapper.findByVehicleNum(Page.of(current,size),vehicle.getVehicleNum());
        }
        if (vehicle.getDriverName() != null && !vehicle.getDriverName().equals("")){
            return vehicleMapper.findByDriverName(Page.of(current,size),vehicle.getDriverName());
        }
        return vehicleMapper.findByCarrierName(Page.of(current,size),vehicle.getCarrierName());
    }

    @Override
    public List<Vehicle> findReadyExamData() {
        return vehicleMapper.findReadyExamData();
    }

    @Override
    public int passExam(User user,Integer vehicleId) {
       return vehicleMapper.examDriver(user,vehicleId);
    }

    @Override
    public int failExam(Integer vehicleId) {
        return vehicleMapper.deleteById(vehicleId);
    }
}
